/*
 * microprocessor_commands.h
 *
 *  Created on: 2017年9月22日
 *      Author: shine
 */

#ifndef MICROPROCESSOR_COMMANDS_H_
#define MICROPROCESSOR_COMMANDS_H_



#define UR_PROTOCOL_CODE 0x5255

#define ILLEGAL_COMMAND 0 /* Use EMPTY_COMMAND for no command */

#define POSITION_BIT (1<<0)                        /*   1 */
#define SPEED_BIT (1<<1)                           /*   2 */
#define ACCELERATION_BIT (1<<2)                    /*   4 */
#define CURRENT_BIT (1<<3)                         /*   8 */
#define SOFTNESS_BIT (1<<4)                        /*  16 */
#define ACCELEROMETER_BIT (1<<5)                   /*  32 */
#define LOWSPEED_COMM_BIT (1<<6)                   /*  64 */
#define SPECIAL_COMMAND_BIT (1<<7)                 /* 128 */

/* These are special commands to the joints*/
#define EMERGENCY_STOP_COMMAND (SPECIAL_COMMAND_BIT|0)
#define GOTO_BOOTLOADER_COMMAND (SPECIAL_COMMAND_BIT|1)
#define EMPTY_COMMAND (SPECIAL_COMMAND_BIT|2)
#define GOTO_FREEDRIVE_COMMAND (SPECIAL_COMMAND_BIT|3)
#define START_NORMAL_OPERATION (SPECIAL_COMMAND_BIT|4)
#define CALIBRATION_DATA_WRITE (SPECIAL_COMMAND_BIT|5)
#define CALIBRATE_JOINT_NOW (SPECIAL_COMMAND_BIT|6)
#define CALIBRATION_DATA (SPECIAL_COMMAND_BIT|7)
#define CALIBRATION_DATA_REQUEST (SPECIAL_COMMAND_BIT|8)
#define BOOTLOADER_MESSAGE (SPECIAL_COMMAND_BIT|9)
#define REINITIALIZE_MOTOR_DRIVER (SPECIAL_COMMAND_BIT|10)
#define SEND_FULL_POSITION_REQUEST (SPECIAL_COMMAND_BIT|11)
#define PROGRAM_DATA_FOR_BOOTLOADER (SPECIAL_COMMAND_BIT|12)
#define CLEAR_CALIBRATION_DATA (SPECIAL_COMMAND_BIT|13)
#define GOTO_BOOTLOADER_WRITER_COMMAND (SPECIAL_COMMAND_BIT|14)
#define SET_ZERO_POSITION_COMMAND (SPECIAL_COMMAND_BIT|15)
#define SECURITY_STOP_COMMAND (SPECIAL_COMMAND_BIT|16)
#define REWRITE_CALIBRATION_DATA_TO_FLASH_COMMAND (SPECIAL_COMMAND_BIT|17)
#define PLEASE_REQUEST_CALIBRATION_DATA_COMMAND (SPECIAL_COMMAND_BIT|18)
#define STOP_SENDING_CALIBRATION_DATA_COMMAND (SPECIAL_COMMAND_BIT|19)
#define FULL_POSITION_MESSAGE (SPECIAL_COMMAND_BIT|20)
#define TOOL_IO_COMMAND (SPECIAL_COMMAND_BIT|21)
#define SET_SAFETY_WINDOW_FRACTION (SPECIAL_COMMAND_BIT|22)
#define SET_JOINT_ID_COMMAND (SPECIAL_COMMAND_BIT|23)
#define GOTO_BACKDRIVE_MODE_COMMAND (SPECIAL_COMMAND_BIT|24)
#define PART_D_CALIBRATION (SPECIAL_COMMAND_BIT|25)
#define GOTO_IDLE_COMMAND (SPECIAL_COMMAND_BIT|26)
#define SHUTDOWN_COMMAND (SPECIAL_COMMAND_BIT|27)
#define UNDEFINED_COMMAND (SPECIAL_COMMAND_BIT|28)
#define REMOVE_EUROMAP67 (SPECIAL_COMMAND_BIT|29)
#define MAIN_POWER_OFF (SPECIAL_COMMAND_BIT|30)
#define DISCONNECT_IO_AND_24V (SPECIAL_COMMAND_BIT|31)
#define CONNECT_IO_AND_24V (SPECIAL_COMMAND_BIT|32)
#define PROTECTIVE_STOP (SPECIAL_COMMAND_BIT|33)
#define RESET_SAFEGUARD (SPECIAL_COMMAND_BIT|34)
#define FORCE_FAULT (SPECIAL_COMMAND_BIT|35)
#define REBOOT (SPECIAL_COMMAND_BIT|36)
#define REMOVE_TEACH_PENDANT (SPECIAL_COMMAND_BIT|37)
#define START_SENDING_ERRORS_FROM_BUFFER_TO_CONTROLLER (SPECIAL_COMMAND_BIT|38)
#define REGULATION_PARAMETER (SPECIAL_COMMAND_BIT|39)
#define SET_SPECIAL_PARAMETER (SPECIAL_COMMAND_BIT|40)

/* These are special parameters*/
#define SPECIAL_PARAMETER_NONE 0
#define SPECIAL_PARAMETER_ANGLE_OFFSET 1

/* These are not errors, but joint modes. */
#define JOINT_MODE_BAR 237 /* Defines where values used by joint modes start */
#define JOINT_PART_D_CALIBRATION_MODE 237
#define JOINT_BACKDRIVE_MODE 238
#define JOINT_POWER_OFF_MODE 239
#define JOINT_EMERGENCY_STOPPED_MODE 240
#define JOINT_CALVAL_INITIALIZATION_MODE 241
#define JOINT_ERROR_MODE 242
#define JOINT_FREEDRIVE_MODE 243
#define JOINT_SIMULATED_MODE 244 /* Probably not used besides inside PC */
#define JOINT_NOT_RESPONDING_MODE 245
#define JOINT_MOTOR_INITIALISATION_MODE 246
#define JOINT_BOOTING_MODE 247
#define JOINT_PART_D_CALIBRATION_ERROR_MODE 248
#define JOINT_BOOTLOADER_MODE 249
#define JOINT_CALIBRATION_MODE 250
#define JOINT_SECURITY_STOPPED_MODE 251
#define JOINT_FAULT_MODE 252
#define JOINT_RUNNING_MODE 253
#define JOINT_INITIALISATION_MODE 254
#define JOINT_IDLE_MODE 255

#define MASTER_RUNNING_MODE JOINT_RUNNING_MODE
#define MASTER_INITIALISATION_MODE JOINT_INITIALISATION_MODE
#define MASTER_FAULT_MODE JOINT_FAULT_MODE
#define MASTER_ERROR_MODE JOINT_ERROR_MODE
#define MASTER_BOOTLOADER_MODE JOINT_BOOTLOADER_MODE

#define NODE_FAULT_MODE JOINT_FAULT_MODE
#define NODE_ERROR_MODE JOINT_ERROR_MODE
#define NODE_NOT_RESPONDING_MODE JOINT_NOT_RESPONDING_MODE
#define NODE_BOOTLOADER_MODE JOINT_BOOTLOADER_MODE
#define NODE_ACTIVE_MODE JOINT_RUNNING_MODE
#define NODE_OFF_MODE JOINT_IDLE_MODE
#define NODE_EMERGENCY_STOPPED_MODE JOINT_EMERGENCY_STOPPED_MODE

#define TOOL_FAULT_MODE JOINT_FAULT_MODE
#define TOOL_ERROR_MODE JOINT_ERROR_MODE
#define TOOL_NOT_RESPONDING_MODE JOINT_NOT_RESPONDING_MODE
#define TOOL_BOOTLOADER_MODE JOINT_BOOTLOADER_MODE
#define TOOL_ACTIVE_MODE JOINT_RUNNING_MODE
#define TOOL_OFF_MODE JOINT_IDLE_MODE

#define IO_ANALOG_INPUT_RANGE_0_TO_5_VOLT 0x00
#define IO_ANALOG_INPUT_RANGE_5_TO_5_VOLT 0x01
#define IO_ANALOG_INPUT_RANGE_0_TO_10_VOLT 0x02
#define IO_ANALOG_INPUT_RANGE_10_TO_10_VOLT 0x03

/* Events from PC to masterboard (Events send by the ond way (Masterboard Special Command) will overwride this message)  */
enum PC_TO_MASTERBOARD_SPECIAL_EVENTS {
  PC_TO_MASTERBOARD_SPECIAL_EVENT_POWER_ROBOT_OFF       						  = SHUTDOWN_COMMAND,         							/* (Masterboard special command EMERGENCY_STOP_COMMAND - WITHOUT SPECIAL_COMMAND_BIT)  */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_GOTO_BOOTLOADER       						  = GOTO_BOOTLOADER_COMMAND,  							/* (Masterboard special command GOTO_BOOTLOADER_COMMAND - WITHOUT SPECIAL_COMMAND_BIT) */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_NO_EVENT              						  = EMPTY_COMMAND,            							/* (Masterboard special command EMPTY_COMMAND - WITHOUT SPECIAL_COMMAND_BIT) */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_UNDEFINED             						  = UNDEFINED_COMMAND,        							/* Communication with PC is down */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_POWER_ROBOT_ON        						  = START_NORMAL_OPERATION,   							/* (Masterboard special command START_NORMAL_OPERATION - WITHOUT SPECIAL_COMMAND_BIT) */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_FAULT                 						  = FORCE_FAULT,              							/* PC forces the masterbaord to FAULT */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_REBOOT                						  = REBOOT,                   							/* PC forces the masterbaord to Reboot (only active after some time, if the safety state is FAULT) - This is the only way to leave SAFETY_STATE_BOOTLOADER*/
  PC_TO_MASTERBOARD_SPECIAL_EVENT_RESET_SAFEGUARD       						  = RESET_SAFEGUARD,          							/* This message may only come when the safeguard interface is in "waiting for reset" */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_PROTECTIVE_STOP       						  = PROTECTIVE_STOP,          							/* Event "force" masterboard to safeguard state if OK */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_CONNECT_IO_AND_24V    						  = CONNECT_IO_AND_24V,       							/* Connect the internal 24V PSU and the 24V I/O (DO's) */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_DISCONNECT_IO_AND_24V 						  = DISCONNECT_IO_AND_24V,    							/* Disconnect the internal 24V PSU and the 24V I/O (DO's) */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_MAIN_POWER_OFF        						  = MAIN_POWER_OFF,           							/* Turn the main power off the masterboard + PC. (Hard power off) */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_REMOVE_EUROMAP67      						  = REMOVE_EUROMAP67,         							/* Safely remove a euromap67 interface from a controlbox */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_REMOVE_TEACH_PENDANT  						  = REMOVE_TEACH_PENDANT,      							/* Safely remove the screen safety functionality from a controlbox */
  PC_TO_MASTERBOARD_SPECIAL_EVENT_START_SENDING_ERRORS_FROM_BUFFER_TO_CONTROLLER  = START_SENDING_ERRORS_FROM_BUFFER_TO_CONTROLLER      /* This command is sent from the controller when ethernet communication is running and the masterboard must start sending errors from its buffer to the controller */
};



#endif /* MICROPROCESSOR_COMMANDS_H_ */
